﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Vega.USiteBuilder;
using System.Text;
using SPCFMedicineInformer.Constants;

namespace SPCFMedicineInformer.Helpers
{
    public class FormValidation
    {
        public static bool ValidateRegistration(Literal lit, string firstname, string lastname, string newPW,string confirmPW, string email,int id,bool isNewPassword,string gender)
        {
            var sb = new StringBuilder();
            var msgFormat = "<p class=\"{0}\">{1}</p>";
            var isValidSignup = true;
            var currUser = id > 0 ? MemberHelper.GetMemberById(id) : null;
            var currEmail = currUser != null ? currUser.Email : "";
            if (string.IsNullOrEmpty(firstname) || string.IsNullOrEmpty(lastname))
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Firstname & Lastname are required"));
            }
            if (string.IsNullOrEmpty(email))
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Email is required"));
            }
            else if (string.IsNullOrEmpty(currEmail) || email != currEmail)
            {
                var currentMember = MemberHelper.GetMemberFromLoginName(email);
                if (currentMember == null)
                    currentMember = MemberHelper.GetMemberFromEmail(email);
                if (currentMember != null)
                {
                    isValidSignup = false;
                    sb.AppendLine(string.Format(msgFormat, "msg error", "Email already Exist"));
                }
            }
            if (isNewPassword && newPW != confirmPW )
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Password does not match."));
            }
            if (isNewPassword && (string.IsNullOrEmpty(newPW) || string.IsNullOrEmpty(confirmPW)))
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Password cannot be empty."));
            }
            if (string.IsNullOrEmpty(gender))
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Gender is required."));
            }

            if (!isValidSignup)
            {
                lit.Visible = true;
                lit.Text = sb.ToString();
            }
            return isValidSignup;
        }
    }
}